import QtQuick 2.15
import QtQuick.Layouts 1.15
import QtQuick.Controls 2.15
import QtQuick.Shapes 1.15

MainView {
    id: root
    width: 800
    height: 391
    property color btnColor: "#7080C0"
    property int btnHeight: 50
    property int btnWidth: 110
    Component.onDestruction: {
       funcTimer.destroy()
    }
    function savePara()
    {
        var parityValue = 0;
        if(cmbParity.displayText===qsTr("奇校验")){
            parityValue=2
        }
        else if(cmbParity.displayText===qsTr("偶校验")){
            parityValue=1
        }
        var result = mymodbus.slotSaveComParas(cmbComName.displayText,Number(cmbBaudrate.displayText),Number(cmbDataBit.displayText),cmbStopBit.currentIndex,parityValue, cmbType.currentIndex)
        if(result)
        {
            //comSettingSaveOK=true;
            commParaSaveOKDialog.title=qsTr("保存成功")
            commParaSaveOKDialog.open();
            commParaSaveOKDialog.accepted.connect(function() {
                commParaSaveOKDialog.close()

            })
        }
        else
        {
            commParaSaveOKDialog.title=qsTr("保存失败")
            commParaSaveOKDialog.accepted.connect(function() {
                commParaSaveOKDialog.close()
            })
        }
        loaderText.visible =false
        commParaSaveOKDialog.open()
    }

    function getParityIndex(parityIndex){
        var index = -1;
        if(parityIndex === 1)
        {
            //return qsTr("偶校验")
            index = 1
        }
        else if(parityIndex === 2)
        {
            index = 2
        }
        else
        {
            index = 0
        }
        return index
    }

    MyBuzyIndicator{
        anchors.centerIn: parent
        id: commParaLoader
        visible:false
        z:101
    }
    MyComboBox
    {
        id:cmbType
        x: 334
        y: 32
        width: btnWidth
        height: btnHeight
        borderColor:"white"
        fillColor:"#0A3653"

        model:[qsTr("仪表"),"4G"]
        currentIndex: 0
        onActivated: {
            if(cmbType.currentIndex === 0){
                cmbComName.currentIndex = cmbComName.find(mymodbus.comName);
                cmbBaudrate.currentIndex = cmbBaudrate.find(mymodbus.baudrate);
                cmbDataBit.currentIndex = cmbDataBit.find(mymodbus.dataBit);
                cmbStopBit.currentIndex = mymodbus.stopBit;
                cmbParity.currentIndex =getParityIndex(mymodbus.parity)
            }
            else if(cmbType.currentIndex === 1){
                cmbComName.currentIndex = cmbComName.find(mymodbus.comName4g);
                cmbBaudrate.currentIndex = cmbBaudrate.find(mymodbus.baudrate4g);
                cmbDataBit.currentIndex = cmbDataBit.find(mymodbus.dataBit4g);
                cmbStopBit.currentIndex = mymodbus.stopBit4g;
                cmbParity.currentIndex =getParityIndex(mymodbus.parity4g)
            }
        }
    }
    Component.onCompleted: {
        cmbComName.currentIndex = cmbComName.find(mymodbus.comName);
        cmbBaudrate.currentIndex = cmbBaudrate.find(mymodbus.baudrate);
        cmbDataBit.currentIndex = cmbDataBit.find(mymodbus.dataBit);
        cmbStopBit.currentIndex = mymodbus.stopBit;
        cmbParity.currentIndex =getParityIndex(mymodbus.parity)
    }
    GridLayout{
        id: generalSettingsLayout
        rows: 2
        columns: 6
        anchors.centerIn: parent
        rowSpacing: 40
        Button
        {
            Layout.row:0
            Layout.column:0
            Layout.preferredWidth: btnWidth
            Layout.preferredHeight: btnHeight
            text: qsTr("串口号")
            font.pixelSize: 18
            background: Rectangle{
                color: btnColor
                    }
            onClicked: {
                cmbComName.popup.open()
            }
        }

       MyComboBox
       {
           id:cmbComName
           Layout.row:0
           Layout.column:1
           Layout.preferredWidth: btnWidth
           Layout.preferredHeight: btnHeight
           borderColor:"white"
           fillColor:"#0A3653"

           model:mymodbus.comData
           currentIndex: cmbComName.find(mymodbus.comName)
           onActivated: {
           }

       }
        Button
        {
            Layout.row:0
            Layout.column:2
            Layout.preferredWidth: btnWidth
            Layout.preferredHeight: btnHeight
            text: qsTr("波特率")
            font.pixelSize: 18
            background: Rectangle{
                color: btnColor
                    }
            onClicked: {
                cmbBaudrate.popup.open()
            }
        }
        MyComboBox
        {
            id:cmbBaudrate
            Layout.row:0
            Layout.column:3
            Layout.preferredWidth: btnWidth
            Layout.preferredHeight: btnHeight
            borderColor:"white"
            fillColor:"#0A3653"

            model:[9600,19200,38400,57600,115200]
            //displayText: mymodbus.baudrate
            currentIndex:cmbBaudrate.find(mymodbus.baudrate)
            onActivated: {
            }
        }
        Button
        {
            Layout.row:0
            Layout.column:4
            Layout.preferredWidth: btnWidth
            Layout.preferredHeight: btnHeight
            // x: 152
            // y: 13
            text: qsTr("数据位")
            font.pixelSize: 18
            background: Rectangle{
                color: btnColor
                    }
            onClicked: {
                cmbDataBit.popup.open()
            }
        }

        MyComboBox
        {
            id:cmbDataBit
            Layout.row:0
            Layout.column:5
            Layout.preferredWidth: btnWidth
            Layout.preferredHeight: btnHeight
            borderColor:"white"
            fillColor:"#0A3653"


            model:[5,6,7,8]
            currentIndex:cmbDataBit.find(mymodbus.dataBit)
            onActivated: {
            }
        }

        Button
        {
            Layout.row:1
            Layout.column:0
            Layout.preferredWidth: btnWidth
            Layout.preferredHeight: btnHeight
            // x: 152
            // y: 13
            text: qsTr("停止位")
            font.pixelSize: 18
            background: Rectangle{
                color: btnColor
                    }
            onClicked: {
                cmbStopBit.popup.open()
                //miniKeyBoard.lineValue = "";
                //miniKeyBoard.textInput=oldPwd;
            }
        }

        MyComboBox
        {
            id:cmbStopBit
            Layout.row:1
            Layout.column:1
            Layout.preferredWidth: btnWidth
            Layout.preferredHeight: btnHeight
            borderColor:"white"
            fillColor:"#0A3653"
            // background: {
            //     Rectangle: {
            //         color:"transparent"
            //     }
            // }

            model:[1,1.5,2]
            currentIndex: mymodbus.stopBit
            //displayText: mymodbus.stopBit.toString()
            onActivated: {
                cmbStopBit.displayText=cmbStopBit.currentText;
            }
        }

        Button
        {
            Layout.row:1
            Layout.column:2
            Layout.preferredWidth: btnWidth
            Layout.preferredHeight: btnHeight
            // x: 152
            // y: 13
            text: qsTr("校验位")
            font.pixelSize: 18
            background: Rectangle{
                color: btnColor
                    }
            onClicked: {
                cmbParity.popup.open()
                //miniKeyBoard.lineValue = "";
                //miniKeyBoard.textInput=oldPwd;
            }
        }
        MyComboBox
        {
            id:cmbParity
            Layout.row:1
            Layout.column:3
            Layout.preferredWidth: btnWidth
            Layout.preferredHeight: btnHeight
            borderColor:"white"
            fillColor:"#0A3653"

            model:[qsTr("无校验"),qsTr("偶校验"),qsTr("奇校验")]
            currentIndex: getParityIndex(mymodbus.parity)
            onActivated: {
                //cmbParity.displayText=cmbParity.currentText;
            }
        }

    }
    Button
    {
        id:btnCommlOK
        x: 572
        y: 281
        //x: 675
        //y: 20
        width: 100
        height: 100
        background: Rectangle{
            border.width: 0
            color: "#C070B0"
                }

        text: qsTr("确定")
        font.pixelSize: 25
        palette.buttonText: "white"
        onClicked: {
            if(loaderText.visible)
            {
                return;
            }
            loaderText.visible=true;
            funcTimer.start();
        }
    }
    Dialog {
        anchors.centerIn: parent
        id: commParaSaveOKDialog
        //title: "提示"
        standardButtons: Dialog.Ok
        title:qsTr("保存成功！")
        modal: true
        closePolicy: "CloseOnEscape"
        z:100
    }
    Button
    {
        id:btnReturn
        x: 688
        y: 281
        //x: 675
        //y: 20
        width: 100
        height: 100
        background: Rectangle{
            border.width: 0
            color: "#B0B0D0"
                }

        text: qsTr("返回")
        font.pixelSize: 25
        palette.buttonText: "white"
        onClicked: {
            bottomBar.selected=4
            myLoader.source="SettingsPanel.qml"
        }
    }
    Timer {
        id:funcTimer
        running: false
        repeat: false
        interval:100
        onTriggered: {
            savePara()
        }
    }
}
